﻿using MyTRCP.Common;

namespace MyTRCP.Common.Models
{
    public class DevStatus : BitWalkableObj
    {
        public bool PowerOn { get; set; }
        public bool InTx { get; set; }
        public bool SQ { get; set; }
        public bool InRx { get; set; }
        public DoubleChannelTypeEnum DoubleChannel { get; set; }//2
        public bool Scan { get; set; }
        public bool Radio { get; set; }
        public byte CurrentChannel { get; set; }//4   low 4 bit
        public bool GPSLocked { get; set; }
        public bool HfpConnected { get; set; }
        public bool AocConnected { get; set; }
        //skip bit 1
        //standard end.extra begin
        public byte RSSIBase { get; set; }//4
        public byte Region { get; set; }//6
                                        //p8 high 4 bit
        public DevStatus(byte[] data) : base(data)
        {
        }
        public int RSSI
        {
            get
            {
                return RSSIBase * 100 / 15;
            }
        }
        public override string ToString()
        {
            return $"powerOn: {(PowerOn ? 1 : 0)}; inTx: {(InTx ? 1 : 0)}; inRx:{(InRx ? 1 : 0)}; sq: {(SQ ? 1 : 0)}; scan: {(Scan ? 1 : 0)}; radio:{(Radio ? 1 : 0)}; gps_locked: {(GPSLocked ? 1 : 0)}; hfp_connected: {(HfpConnected ? 1 : 0)}; aoc_connected: {(AocConnected ? 1 : 0)}; current_ch:{CurrentChannel}; doubleChannel: {DoubleChannel}; rssi: {RSSI}; region: {Region};";
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                .WriteBit(PowerOn)
                .WriteBit(InTx)
                .WriteBit(SQ)
                .WriteBit(InRx)
                .WriteBits((byte)DoubleChannel, 2)
                .WriteBit(Scan)
                .WriteBit(Radio)
                .WriteBits(CurrentChannel, 4)
                .WriteBit(GPSLocked)
                .WriteBit(HfpConnected)
                .WriteBit(AocConnected)
                .SkipBits(1)
                .WriteBits(RSSIBase, 4)
                .WriteBits(Region, 6)
                .WriteBits(CurrentChannel >> 4, 4);
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 2)
                throw new ArgumentException("字节数组长度过小（2）", nameof(bw));
            PowerOn = bw.ReadBit();
            InTx = bw.ReadBit();
            SQ = bw.ReadBit();
            InRx = bw.ReadBit();
            DoubleChannel = (DoubleChannelTypeEnum)bw.ReadBits(2);
            Scan = bw.ReadBit();
            Radio = bw.ReadBit();
            CurrentChannel = (byte)bw.ReadBits(4);
            GPSLocked = bw.ReadBit();
            HfpConnected = bw.ReadBit();
            AocConnected = bw.ReadBit();
            bw.SkipBits(1);
            if (bw.ByteLen >= 4)
            {
                RSSIBase = (byte)bw.ReadBits(4);
                Region = (byte)bw.ReadBits(6);
                CurrentChannel |= (byte)(bw.ReadBits(4) << 4);
            }
            return bw;
        }
    }
}